package com.aat.dxfy.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ResultThread {

	private static ExecutorService threadPool = Executors.newFixedThreadPool(10);

	public static void main(String[] args) {
		int i=execute(new Runnable() {
			@Override
			public void run() {
				for (int j = 0; j < 100; j++) {
					System.out.println(j);
					try {
						Thread.sleep(j);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		System.out.println("res="+i);
		threadPool.shutdown();
	}

	public static Integer execute(final Runnable command) {
		Integer i = 0;
		try {
			Callable<Integer> task = new Callable<Integer>() {
				@Override
				public Integer call() throws Exception {
					command.run();
					return 1;
				}
			};

			Future<Integer> res = threadPool.submit(task);
			i = res.get();
			
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}

		return i;
	}

}
